﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageWorkflow;

namespace SimpleConsoleExamples.Examples
{
    class OneWayExceptionHandlingExample : IExample
    {
        public void Run()
        {
            WorkflowChannel channel = new WorkflowChannel();

            var handler1 = WorkflowHandlerFactory.FromLambda<string>(x => Console.WriteLine("InnerHandler 1 received: {0}", x));
            var handler2 = WorkflowHandlerFactory.FromLambda<string>(x => Console.WriteLine("InnerHandler 2 received: {0}", x));
            var handler3 = WorkflowHandlerFactory.FromLambda<string>(x =>
            {
                throw new InvalidOperationException("Test ex thrown from handler 3");
            });
            var handler4 = WorkflowHandlerFactory.FromLambda<string>(x => Console.WriteLine("InnerHandler 4 received: {0}", x));            

            channel.Subscribe(new WorkflowHandlerConfiguration(handler1)
            {
                Successors = new[]
                {
                    new WorkflowHandlerConfiguration(handler2)
                    {
                        Successors = new[]
                        {
                            new WorkflowHandlerConfiguration(handler4)
                        }
                    },
                    new WorkflowHandlerConfiguration(handler3)
                }
            });

            try
            {
                channel.Dispatch("test");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception found: {0}", ex.Message);
            }
        }
    }
}
